package com.crm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.crm.entity.Askers;
import com.crm.entity.User;
import com.crm.repository.UserRepository;
import com.crm.service.AskersService;
import com.crm.util.FenyeUtil;
import com.crm.util.ResultInfoUtil;
/**
 * 此controller为咨询师的
 * @author hp 
 *
 */
@RestController
@RequestMapping("asker")
public class AskersController {

	@Autowired
	private AskersService askerff;
	@Autowired
	private UserRepository userff;
	/**
	 * 通过分页模糊查询数据
	 * @param ask
	 * @param page 页数(自动)，默认10条数据每页
	 * @return 返回值为分页工具类的json数据格式
	 */
	@GetMapping
	public FenyeUtil<Askers> GetAskerByPage(Askers ask,Integer page){
		return askerff.FindAskerByPage(ask, page);
	}
	@PostMapping
	public String SaveAndFulsh(Askers ask,String username) {
		User user=userff.findByUserName(username);
		if(user!=null) {
			ask.setUse(user);
			return askerff.UpdAndSaveAsk(ask);
		}else {
			return "用户名可能不存在！！失败了";
		}
	}
	@PutMapping
	public String SaveAndFulsh1(Askers ask,String username) {
		User user=userff.findByUserName(username);
		if(user!=null) {
			ask.setUse(user);
			return askerff.UpdAndSaveAsk(ask);
		}else {
			return "修改发生了错误！！失败了";
		}
	}
	//查询用户详细信息
	@GetMapping
	@RequestMapping("getuser")
	public User gets(String username) {
		return userff.findByUserName(username);
	}
	@DeleteMapping
	public String Del(Integer aid) {
		return askerff.DelAsk(aid);
	}
}
